/*
   Copyright (c) 2015 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2015-02-07 Marco Riccardi - initial implementation
 *  
 */

package org.mizartools.dli.database;

import java.io.File;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Set;

import org.mizartools.dli.DecodedLibraryItem;
import org.mizartools.dli.DliParseException;
import org.mizartools.dli.ItemType;
import org.mizartools.dli.utility.Datafile;
import org.mizartools.dli.utility.DirectoryFactory;

public class FunctorNotation {

	public static final ItemType ITEM_TYPE = ItemType.funcnot;
	private static LinkedHashMap<String, org.mizartools.dli.FunctorNotation> dliData;
	private static LinkedHashMap<String, String> stringData;
	
	private FunctorNotation(){}
	
	private static LinkedHashMap<String, String> getStringDataMap() {
		if (stringData == null) {
			String fileName = ITEM_TYPE.toString() + ".dli";
			try {
				File file = new File(DirectoryFactory.getDliDatabaseDirectory(), fileName);
				stringData = Datafile.getDliStringLinkedHashMap(file);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return stringData;
	}
	
	public static int getItemNumber(){
		return getStringDataMap().size();
	}

	public static Set<String> getItemIdList() {
		return getStringDataMap().keySet();
	}

	public static Collection<String> getItemList() {
		return getStringDataMap().values();
	}
	
	public static org.mizartools.dli.FunctorNotation getFunctorNotation(String itemId) {
		if (dliData == null) {dliData = new LinkedHashMap<String, org.mizartools.dli.FunctorNotation>();}
		if (dliData.containsKey(itemId)) {
			return dliData.get(itemId);
		} else {
			if (getStringDataMap().containsKey(itemId)) {
				DecodedLibraryItem decodedLibraryItem;
				try {
					decodedLibraryItem = DecodedLibraryItem.newInstance(getStringDataMap().get(itemId));
					dliData.put(itemId, (org.mizartools.dli.FunctorNotation) decodedLibraryItem.getItemDefinition());
					return dliData.get(itemId);
				} catch (DliParseException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}

}
